package com.icss.train_sys_back.service.ticket_manage.Impl;

import com.icss.train_sys_back.enumEntity.PriceCalculationConstants;
import com.icss.train_sys_back.entity.home.MyTicketEntity;
import com.icss.train_sys_back.entity.ticket_manage.TicketEntity;
import com.icss.train_sys_back.entity.train_manage.StationEntity;
import com.icss.train_sys_back.mapper.ticket_manage.TicketManageMapper;
import com.icss.train_sys_back.mapper.train_manage.StationMapper;
import com.icss.train_sys_back.service.ticket_manage.TicketManageService;
import com.icss.train_sys_back.utils.PositionUtil;
import com.icss.train_sys_back.enumEntity.SeatTypePriceAdjustment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

/**
 * @Author 小泽
 * @createTime 2025/10/2 14:15
 */
@Service
public class TicketManageServiceImpl implements TicketManageService {
    @Autowired
    private TicketManageMapper ticketManageMapper;
    @Autowired
    private StationMapper stationMapper;

    //车票分页+条件查询
    @Override
    public List<TicketEntity> getListConditions(String trainNumberId, String startStationId, Integer endStationId, Integer status) {
        return ticketManageMapper.getListConditions(trainNumberId, startStationId, endStationId, status);
    }

    //更新车票信息
    @Override
    public void update(TicketEntity ticket) {
        ticketManageMapper.update(ticket);
    }

    //更新车票状态
    @Override
    public boolean changeStatus(String ticketId, Integer ticketStatus) {
        return ticketManageMapper.changeStatus(ticketId, ticketStatus);
    }

    //删除车票
    @Override
    public boolean deleteByTicketId(String ticketId) {
        return ticketManageMapper.delete(ticketId);
    }

    //添加车票
    @Override
    public boolean add(TicketEntity ticket) {
        //生成车票号  T + 日期 + 随机四位数
        String ticketId;
        LocalDate today = LocalDate.now();
        // 格式化日期为yyyyMMdd格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String dateStr = today.format(formatter);
        // 生成随机四位数（0000-9999）
        Random random = new Random();
        int randomNum = 1000 + random.nextInt(9000); // 确保是四位数
        String randomStr = String.format("%04d", randomNum); // 格式化确保四位数，不足前面补零
        // 组合生成车票号
        ticketId = "T" + dateStr + randomStr;

        ticket.setTicketId(ticketId);

        //计算票价 （终点站 -- 始发站  相距公里数 * 0.48）
        StationEntity startStation = stationMapper.getStationBySid(ticket.getStartStationId());
        StationEntity arrivalStation = stationMapper.getStationBySid(ticket.getArrivalStationId());

        Integer length = PositionUtil.getDistance(startStation.getGlobalX(), startStation.getGlobalY(), arrivalStation.getGlobalX(), arrivalStation.getGlobalY());

        BigDecimal ticketMoney = BigDecimal.valueOf(length * PriceCalculationConstants.DISTANCE_COEFFICIENT.getValue());
        //根据座位类型计算二次票价
        SeatTypePriceAdjustment seatType = SeatTypePriceAdjustment.getByCode(ticket.getCarriageType());
        ticketMoney = ticketMoney.add(BigDecimal.valueOf(seatType.getAdjustmentAmount()));

        ticket.setTicketPrice(ticketMoney);

        return ticketManageMapper.add(ticket);
    }

    // 根据城市获取列车票务信息
    @Override
    public List<TicketEntity> getTrainTickets(String startCity, String endCity, String goDate) {
        return ticketManageMapper.getTrainTickets(startCity, endCity, goDate);
    }

    //查询本人车票
    @Override
    public List<MyTicketEntity> listByUserId(Integer userId) {
        return ticketManageMapper.listByUserId(userId);
    }

    // 批量插入
    @Override
    public void batchAdd(List<TicketEntity> ticketEntityList) {
        ticketManageMapper.batchAdd(ticketEntityList);
    }

}
